import axiosInstance from '@/utils/request'

/**
 * 创建订单
 * @param {Object} ordersDTO 订单数据
 * @returns {Promise} 包含订单信息的Promise
 */
export const createOrder = (ordersDTO) => {
    return axiosInstance.post('/product/orders/create', ordersDTO)
}

/**
 * 根据用户ID和状态查询订单列表
 * @param {Number} userId 用户ID
 * @param {Number} [status] 订单状态(可选)
 * @returns {Promise} 包含订单列表的Promise
 */
export const getOrdersByUserAndStatus = (userId, status) => {
    return axiosInstance.get('/product/orders/list', {
        params: {
            userId,
            status
        }
    })
}

/**
 * 分页查询订单
 * @param {Object} queryParams 查询参数
 * @param {Number} queryParams.userId 用户ID
 * @param {Number} [queryParams.orderStatus] 订单状态
 * @param {Number} [queryParams.orderId] 订单ID
 * @param {String} [queryParams.sortField] 排序字段
 * @param {String} [queryParams.sortOrder] 排序顺序
 * @param {String} [queryParams.startTime] 开始时间
 * @param {String} [queryParams.endTime] 结束时间
 * @param {Number} [queryParams.current] 当前页码
 * @param {Number} [queryParams.size] 每页大小
 * @returns {Promise} 包含分页订单数据的Promise
 */
export const getOrdersPaged = (queryParams) => {
    return axiosInstance.get('/product/orders/page', {
        params: queryParams
    })
}

/**
 * 获取订单详情
 * @param {Number} orderId 订单ID
 * @returns {Promise} 包含订单详情的Promise
 */
export const getOrderDetail = (orderId) => {
    return axiosInstance.get(`/product/orders/detail/${orderId}`)
}

/**
 * 取消订单
 * @param {Number} orderId 订单ID
 * @returns {Promise} 包含操作结果的Promise
 */
export const cancelOrder = (orderId) => {
    return axiosInstance.post(`/product/orders/cancel/${orderId}`)
}

/**
 * 确认收货
 * @param {Number} orderId 订单ID
 * @returns {Promise} 包含操作结果的Promise
 */
export const confirmReceipt = (orderId) => {
    return axiosInstance.post(`/product/orders/confirm/${orderId}`)
}


/**
 * 申请售后
 * @param {Number} orderId 订单ID
 * @param {String} reason 售后原因
 * @returns {Promise} 包含操作结果的Promise
 */
export const applyAfterSale = (orderId, reason) => {
    return axiosInstance.post(`/product/orders/after-sale`, {
        orderId,
        reason
    })
}

/**
 * 获取用户订单各状态数量统计
 * @param {Number} userId 用户ID
 * @returns {Promise} 包含订单统计的Promise
 */
export const getOrderStatusCounts = (userId) => {
    return axiosInstance.get('/product/orders/status-counts', {
        params: { userId }
    })
}



export default {
    createOrder,
    getOrdersByUserAndStatus,
    getOrdersPaged,
    getOrderDetail,
    cancelOrder,
    confirmReceipt,
    applyAfterSale,
    getOrderStatusCounts
}